(ns picture-gallery.views.layout
  (:require [hiccup.page :refer [html5 include-css]]
            [hiccup.element :refer [link-to]]
            [hiccup.form :refer :all]
            [noir.validation :as vali]
            [noir.session :as session]
            [noir.util.anti-forgery :refer :all]))

(defn error-item [[error]]
  [:div.error error])

(defn make-menu [& items]
  [:div (for [item items] [:div.menuitem item])])

(defn guest-menu []
  (make-menu
    (link-to "/" "主页")
    (link-to "/register" "注册")
    (vali/on-error :login error-item)
    (form-to [:post "/login"]
           (anti-forgery-field)
           (text-field {:placeholder "screen name"} "id")
           (password-field {:placeholder "password"} "pass")
           (submit-button "登陆"))))

(defn user-menu [user]
  (make-menu
    (str (session/get :user))
    (link-to "/" "主页")
    (link-to "/upload" "上传照片")
    (link-to "/logout" "登出")))

(defn base [& body]
  (html5
    [:head
     [:title "Welcome to picture-gallery"]
     (include-css "/css/screen.css")]
    [:body body]))

(defn common [& content]
  (base
    (if-let [user (noir.session/get :user)]
      (user-menu user)
      (guest-menu))
    content))